#include "gameSystem.h"

#include <list>

//--------------------------------------------------------------------------------------------------

static list<gameSystem*> gGameSystems;

//--------------------------------------------------------------------------------------------------

void gameSystem_register(gameSystem* system)
{
	gGameSystems.push_back(system);
}

void gameSystem_updateAll()
{
	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		system->update(ofGetLastFrameTime());
	}
}

void gameSystem_setupAll()
{
	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		system->setup();
	}
}

void gameSystem_drawAll()
{
	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		system->draw();
	}

	// Disable fog for overlay rendering
	bool fog = glIsEnabled(GL_FOG);

	if(fog)
		glDisable(GL_FOG);

	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		system->drawOverlay();
	}

	if(fog)
		glEnable(GL_FOG);
}

void gameSystem_keyPressed(int key)
{
	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		system->keyPressed(key);
	}
}

void gameSystem_keyReleased(int key)
{
	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		system->keyReleased(key);
	}
}

void gameSystem_windowResized(int w, int h)
{
	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		system->windowResized(w, h);
	}
}

void gameSystem_mouseMoved (int x, int y)
{
	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		system->mouseMoved(x, y);
	}
}

void gameSystem_mouseDragged (int x, int y, int button)
{
	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		if(system->mouseDragged(x, y, button))
			break;
	}
}

void gameSystem_mousePressed (int x, int y, int button)
{
	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		if(system->mousePressed(x, y, button))
			break;
	}
}

void gameSystem_mouseReleased (int x, int y, int button)
{
	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		system->mouseReleased(x, y, button);
	}
}

void gameSystem_exitAll()
{
	for (std::list<gameSystem*>::iterator it = gGameSystems.begin(); it != gGameSystems.end(); it++)
	{
		gameSystem* system = (gameSystem*)*it;
		system->exit();
	}
}

//--------------------------------------------------------------------------------------------------